﻿namespace Stack;

public class LC0020 {
    public bool IsValid(string s) {
        Stack<char> stack = new Stack<char>();
        char[] charArray = s.ToCharArray();
        foreach (var ch in charArray) {
            if (ch == '(' || ch == '[' || ch == '{') stack.Push(ch);
            else {
                if (ch == ')' && stack.Count > 0 && stack.Pop() == '(') continue;
                if (ch == ']' && stack.Count > 0 && stack.Pop() == '[') continue;
                if (ch == '}' && stack.Count > 0 && stack.Pop() == '{') continue;
                return false;
            }
        }

        return stack.Count == 0;
    }
}